<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags"%>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>短运司机信息</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link href="${pageContext.request.contextPath}/css/bootstrap.min.css?v=3.3.6" rel="stylesheet">
    <link href="${pageContext.request.contextPath}/css/font-awesome.min.css?v=4.4.0" rel="stylesheet">
    <link href="${pageContext.request.contextPath}/css/animate.min.css" rel="stylesheet">
    <link href="${pageContext.request.contextPath}/css/style.min.css?v=4.1.0" rel="stylesheet">
    <link href="${pageContext.request.contextPath}/jQuery-Validation/css/validationEngine.jquery.css" rel="stylesheet">
    <style type="text/css">
        .formErrorContent{color: red;}
    </style>
</head>
<body class="gray-bg">
<div class="formErrorContent"></div>
    <div class="container" style="margin-top: 50px">
        <form class="form-horizontal" role="form" id="editShortDriverForm" autocomplete="off">
                <input type="hidden" name="id" id="driverId">
                <div class="form-group">
                    <label class="col-sm-4 control-label">手机号:</label>
                    <div class="col-sm-4" style="overflow:hidden;">
                        <input type="text" class="form-control validate[required]" name="driverPhone" id="driverPhone" placeholder="手机号" style="width:75%; float:left;">
	                    	<input type="button" id="yzmBtn" value="点击获取验证码" style="width:25%; float:left; height:34px;" >
                    </div>
                </div>
                 <div class="form-group">
                    <label class="col-sm-4 control-label">输入验证码:</label>
                    <div class="col-sm-4">
                        <input type="text" class="form-control validate[required]" name="driverToken" id="driverToken" placeholder="请输入验证码">
                    </div>
                </div>
                <shiro:hasRole name="root">
	                <div class="form-group">
	                    <label class="col-sm-4 control-label">选择分站点:</label>
	                    <div class="col-sm-4">
	                        <select style="width:100%; height:34px; border:0;" class="form-control validate[required]" name="comnumId" id="comnumId">
	                        	<option value="">请选择分站点</option>
	                        </select>
	                    </div>
	                </div>
	            </shiro:hasRole>
                <div class="form-group" style="margin-left: 150px;margin-top: 50px">
                    <div class="control-label">
                        <button type="button" class="btn btn-default" onclick="closeForm();">关闭</button>
                        &nbsp;&nbsp;&nbsp;&nbsp;<button type="submit" class="btn btn-primary"  id="save" >保存</button>
                    </div>
                </div>
         </form>
    </div>
<script src="${pageContext.request.contextPath}/js/jquery.min.js?v=2.1.4"></script>
<script src="${pageContext.request.contextPath}/js/bootstrap.min.js?v=3.3.6"></script>
<script src="${pageContext.request.contextPath}/js/content.min.js?v=1.0.0"></script>
<script src="${pageContext.request.contextPath}/js/plugins/layer/layer.min.js"></script>
<script src="${pageContext.request.contextPath}/js/public/ttbj-core.js"></script>
<script src="${pageContext.request.contextPath}/jQuery-Validation/js/jquery.validationEngine.js"></script>
<script src="${pageContext.request.contextPath}/jQuery-Validation/js/jquery.validationEngine-zh_CN.js"></script>
</body>
<script type="text/javascript">
var shortDriverId;
$(function () {
	shortDriverId = $.getUrlParam("id");
	//点击发送验证码按钮
	document.getElementById("yzmBtn").onclick = function() {
		$.ajax({
	        url: "../driver/send_verification_code.htm",
	        cache: false,
	        type: "get",
	        dataType: "json",
	        data: {"phone":$("#driverPhone").val()},
	        success: function (result) {
	            if (result.status == 0) {
	            	//如果验证码发送成功，则执行60秒倒计时
	            	time(document.getElementById("yzmBtn"));
	                layer.msg(result.message, {icon: 1});
	            } else {
	                layer.msg(result.message, {icon: 2});
	            }
	        }
	    });
	}
	
	//jquery判断某个元素在页面上是否存在
	if($("#comnumId").length > 0){
		//加载分站点的ajax
		$.ajax({
	        url: "../company/getCompanyNumAndNameUsedSelect.htm",
	        cache: false,
	        type: "get",
	        dataType: "json",
	        success: function (result) {
	            if (result.status == 0) {
	            	var data = result.data;
	            	addComnumOptions(data, "#comnumId");
	            }
	        }
	    });
	}
	
    //是否验证成功
    $('#editShortDriverForm').validationEngine('attach', {
        validationEventTrigger : 'blur',	//当提交的时候验证是否合法
        promptPosition: 'inline',
        scroll: false,
        focusFirstField : true,
        onValidationComplete : function (form, valid) {
        	if(valid){
        		if(shortDriverId == 'null'){
        			addShortDriverInfo();
        		}
        	}
        }
    });
});

//关闭窗口
function closeForm() {
    var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
    parent.layer.close(index);
}

var wait = 60;
//发送验证码的倒计时
function time(o) {
	if (wait == 0) {
		o.removeAttribute("disabled");
		o.value = "点击获取验证码";
		wait = 60;
	} else {
		o.setAttribute("disabled", true);
		o.value = "重新发送(" + wait + ")";
		wait--;
		setTimeout(function() {
			time(o)
		}, 1000)
	}
}

//增加
function addShortDriverInfo() {
    $.ajax({
        url: "../driver/addToDriverAndDriverComnumInfo.htm",
        cache: false,
        type: "post",
        dataType: "json",
        data: $("#editShortDriverForm").serialize(),
        beforeSend:function(){var index = layer.load(1, { shade: [0.8,'#ddd']});},
        complete:function(){var index = layer.closeAll("loading");},
        success: function (result) {
            if (result.status == 0) {
                parent.layer.msg(result.message, {icon: 1});
                parent.$table.bootstrapTable('refresh', {url: '../driver/getShortDriverInfoByPage.htm'});
                closeForm();
            } else if(result.status == 71000){	//如果是验证码错误，则不关闭iframe窗口
                layer.msg(result.message, {icon: 2});
            }else {	//其他错误关闭iframe窗口
            	parent.layer.msg(result.message, {icon: 2});
                closeForm();
            }
        }
    });
}

//公司的下拉列表  追加到option中
function addComnumOptions(data, selectId){
	for(var i=0; i<data.length; i++){
		$(selectId).append('<option value="' + data[i].comnumNum + '">' + data[i].comnumName + '</option>');
	}
}
</script>
</html>